package com.example.xmltohtml.controller;

import com.example.xmltohtml.service.PdfToHtmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
@RequestMapping("/pdf")
public class PdfToHtmlController {

    @Autowired
    private PdfToHtmlService pdfToHtmlService;

    /**
     * 显示PDF转换页面
     */
    @GetMapping("/convert")
    public String showConvertPage() {
        return "pdf-convert";
    }

    /**
     * 处理PDF文件上传并转换为HTML
     */
    @PostMapping("/upload")
    public ResponseEntity<String> convertPdfToHtml(@RequestParam("file") MultipartFile file) {
        try {
            if (file.isEmpty()) {
                return ResponseEntity.badRequest().body("请选择PDF文件");
            }

            if (!file.getOriginalFilename().toLowerCase().endsWith(".pdf")) {
                return ResponseEntity.badRequest().body("只支持PDF文件格式");
            }

            String html = pdfToHtmlService.convertPdfToHtml(file);
            
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            headers.setContentDispositionFormData("attachment", 
                file.getOriginalFilename().replace(".pdf", ".html"));
            
            return new ResponseEntity<>(html, headers, HttpStatus.OK);
            
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("转换失败: " + e.getMessage());
        }
    }

    /**
     * 转换项目中的示例PDF文件
     */
    @GetMapping("/convert-sample")
    public ResponseEntity<String> convertSamplePdf() {
        try {
            String pdfPath = "src/main/resources/pdf/H2_AN202505081669303055_1.pdf";
            Path path = Paths.get(pdfPath);
            
            if (!Files.exists(path)) {
                return ResponseEntity.notFound().build();
            }

            String html = pdfToHtmlService.convertPdfToHtml(pdfPath);
            
            // 保存到输出目录
            String outputPath = "output/sample_pdf_converted.html";
            pdfToHtmlService.saveHtmlToFile(html, outputPath);
            
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            
            return new ResponseEntity<>(html, headers, HttpStatus.OK);
            
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("转换失败: " + e.getMessage());
        }
    }

    /**
     * 下载转换后的HTML文件
     */
    @GetMapping("/download/{filename}")
    public ResponseEntity<byte[]> downloadHtml(@PathVariable String filename) {
        try {
            String filePath = "output/" + filename;
            Path path = Paths.get(filePath);
            
            if (!Files.exists(path)) {
                return ResponseEntity.notFound().build();
            }

            byte[] content = Files.readAllBytes(path);
            
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.TEXT_HTML);
            headers.setContentDispositionFormData("attachment", filename);
            
            return new ResponseEntity<>(content, headers, HttpStatus.OK);
            
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
} 